package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import com.alibaba.fastjson.annotation.JSONField;
import com.xcity.db.entity.Player;
import com.xcity.game.adventure.model.ActionTarget;
import com.xcity.game.adventure.model.Foe;
import com.xcity.game.buildv2.BuildV2.State;
import com.xcity.game.common.ItemResponse;
import com.xcity.game.item.ItemInfo;
import com.xcity.game.player.cache.PlayerCacheService;

import naga.x.App;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;

@PomeloMessage(type = Message.Type.RESPONSE)
public class AdventureFoesResponse extends Response {
	
	public byte type; // 0-仇人，1-好友 @see AdventureFoesRequest#type
	public List<FoeInfo> list = Collections.emptyList();
	
	public static class FoeInfo {
		public long id;
		public String name;
		public String icon;
		public byte sex;
		public int level;
		
		public byte cause; // 成为仇人原因
		public ItemInfo item; // 抢走物品
		public int gold; // 偷走现金
	}
	
	public static class LostItem extends ItemResponse {
		public LostItem(int id, int count) {
			super(id, count);
		}
		@JSONField(serialize = false)
		@Override
		public String getDesc() {
			return super.getDesc();
		}
		@JSONField(serialize = false)
		@Override
		public byte getQuality() {
			return super.getQuality();
		}
	}
	
	public AdventureFoesResponse(int serial, byte type) {
		super(serial);
		this.type = type;
	}

	public AdventureFoesResponse(int serial, List<Foe> foes, ActionTarget attackTarget) {
		super(serial);
		type = 0;
		if (foes != null && foes.size() > 0) {
			PlayerCacheService service = App.getApp().getServiceManager().get(PlayerCacheService.class);
			list = new ArrayList<FoeInfo>(foes.size() + 1);
			if (attackTarget != null) { // 随机的攻击目标
				Player player = service.find(attackTarget.getId());
				FoeInfo foe = new FoeInfo();
				foe.id = player.getId();
				foe.name = player.getName();
				foe.icon = player.getIcon();
				foe.sex = player.getSex();
				foe.level = player.getLevel();
				foe.cause = Foe.CAUSE_RANDOM;
				list.add(foe);
			}
			Iterator<Foe> itr = foes.iterator();
			while (itr.hasNext()) {
				Foe ref = itr.next();
				Player player = service.find(ref.getId());
				if (player == null) {
					itr.remove();
					continue;
				}
				if (player.getBuildings2().getLast().getState() != State.BUILDING) {
					continue;
				}
				FoeInfo foe = new FoeInfo();
				foe.id = player.getId();
				foe.name = player.getName();
				foe.icon = player.getIcon();
				foe.sex = player.getSex();
				foe.level = player.getLevel();
				foe.cause = ref.getCause();
				if (foe.cause == Foe.CAUSE_ATTACK) {
					foe.item = new LostItem(ref.getType(), ref.getCount());
				} else {
					foe.gold = ref.getCount();
				}
				list.add(foe);
			}
		}
	}
	
	public AdventureFoesResponse(int serial, byte type, List<Player> players) {
		super(serial);
		this.type = type;
		int size = players.size();
		this.list = new ArrayList<FoeInfo>(size);
		for (int i = 0; i < size; i++) {
			Player player = players.get(i);
			FoeInfo foe = new FoeInfo();
			foe.id = player.getId();
			foe.name = player.getName();
			foe.icon = player.getIcon();
			foe.sex = player.getSex();
			foe.level = player.getLevel();
			foe.cause = Foe.CAUSE_RANDOM;
			list.add(foe);
		}
	}

}
